import React, { Component } from "react";

class Form extends Component {
  state = {
    textValue: "",
  };
  handleChange = (e) => {
    this.setState({
      textValue: e.target.value,
    });
  };
  handleClick = () => {
    // console.log(this.state.textValue);
    if (this.state.textValue) {
      this.props.onGetItem(this.state.textValue);
      this.setState({
        textValue: "",
      });
    }
  };
  handleKeyUp = (e) => {
    if (e.key === "Enter") {
      this.handleClick();
    }
  };
  render() {
    return (
      <div>
        <input
          type="text"
          value={this.state.textValue}
          onChange={this.handleChange}
          onKeyUp={this.handleKeyUp}
        />
        <button onClick={this.handleClick}>新增</button>
      </div>
    );
  }
}

export default Form;
